#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<LL>g[maxn];
LL val[maxn];
LL siz[maxn],son[maxn],sum,rt;
LL dis[maxn],tmp[maxn],cnt=0;
LL ans=0;
bool vis[maxn];
inline void getroot(LL u,LL fa){
     siz[u]=1;son[u]=0;
     for(LL i=0;i<g[u].size();i++){
         LL v=g[u][i];
         if(v==fa||vis[u]) continue;
         dfs(v,u);
         siz[u]+=siz[v];
         if(siz[v]>son[u]) son[u]=siz[v];
     }
     son[u]=max(son[u],sum-siz[u]);
     is( (son[u]<<1)<=sum ) rt=u;
}
inline void getdis(LL u,LL fa){

}
inline void solve(LL u){

}
inline void divide(LL u){
     vis[u]=true;
     solve(u);
     for(LL i=0;i<g[u].size();i++){
         LL v=g[u][i];
         if(vis[v]) continue;
         rt=0;
         son[rt]=sum=siz[v];
         getroot(v,0);
         getroot(rt,0);
         divide(rt);
     }
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++) cin>>val[i];
   for(LL i=1;i<n;i++){
       LL u,v;cin>>u>>v;
       g[u].push_back(v);g[v].push_back(u);
   }
   son[0]=sum=n;
   getroot(1,0);
   getroot(rt,0);
   divide(rt);
   cout<<ans<<"\n";
   return 0;
}
